from system.eventServer import eventServer
from system.object import Object

class Button(Object):
    def __init__(self, parent, type = None, name = None):
        Object.__init__(self, parent, type, name)
        
        self.defaultVar("renderOp2d","rect")
        self.defaultVar("pos",(0,0))
        self.defaultVar("scale",(1,1))
        self.defaultVar("color",(0.75,0.75,0.75))
        self.defaultVar("hasOutline",True)
        
        if self.getVar("hasOutline"):
            outline = self.addChild("outline")
            outline.setVar("renderOp2d","rect")
            outline.setVar("filled",False)
        
        if self.hasVar("text"):
            text = self.addChild("text")
            text.setVar("renderOp2d","text")
            text.setVar("pos",(0,0.8))
            text.setVar("text",self.getVar("text"))
            
        eventServer.register("objectClickUp",self.objectClickUp)
        
    def objectClickUp(self, cmd, args):
        if args["path"] == self.getVar("path"):
            #self.log("clicked")
            if self.hasVar("clickMessage"):
                msgArgs = {}
                if self.hasVar("clickArgs"):
                    msgArgs.update(self.getVar("clickArgs"))
                eventServer.pull(self.getVar("clickMessage"),msgArgs)
            eventServer.pull('buttonClicked',{'path':self.getVar("path"),'x':args['x'],'y':args['y']})
